{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 4 IdVg files with 200L\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R1C1_L200.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R1C3.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R3C1.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R3C3.csv\n",
      "Processing 200L done!\n",
      "Found 4 IdVg files with 300L\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R1C2.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R1C4.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R3C2.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R3C4.csv\n",
      "Processing 300L done!\n",
      "Found 4 IdVg files with 400L\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R2C1.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R2C3.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R4C1.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R4C3.csv\n",
      "Processing 400L done!\n",
      "Found 4 IdVg files with 500L\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R2C2.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R2C4.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R4C2.csv\n",
      "Processing file: E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90\\IdVg_R4C4.csv\n",
      "Processing 500L done!\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import originpro as op\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "import re\n",
    "import glob\n",
    "\n",
    "def origin_shutdown_exception_hook(exctype, value, traceback):\n",
    "    op.exit()\n",
    "    sys.__excepthook__(exctype, value, traceback)\n",
    "\n",
    "if op and op.oext:\n",
    "    sys.excepthook = origin_shutdown_exception_hook\n",
    "\n",
    "# 打开origin界面\n",
    "if op.oext:\n",
    "    op.set_show(True)\n",
    "\n",
    "# 设置路径\n",
    "csv_folder = r'E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20240731_ITO-RF90-10-330_200C30_Ni_tC15\\\\90'\n",
    "working_folder = os.getcwd()\n",
    "template_folder = os.path.join(os.path.abspath('template'))\n",
    "template_path = os.path.join(template_folder, 'IdVg_uniformity_dual-axis.otpu')\n",
    "\n",
    "# 选取数据\n",
    "file_prefix_regions = [\n",
    "    ['IdVg_R1C1', 'IdVg_R1C3', 'IdVg_R3C1', 'IdVg_R3C3'],\n",
    "    ['IdVg_R1C2', 'IdVg_R1C4', 'IdVg_R3C2', 'IdVg_R3C4'],\n",
    "    ['IdVg_R2C1', 'IdVg_R2C3', 'IdVg_R4C1', 'IdVg_R4C3'],\n",
    "    ['IdVg_R2C2', 'IdVg_R2C4', 'IdVg_R4C2', 'IdVg_R4C4']\n",
    "]\n",
    "color_index = ['black', 'red', 'blue', 'green']\n",
    "\n",
    "def process_region(file_prefixes, region_name):\n",
    "    selected_files = []\n",
    "    for prefix in file_prefixes:\n",
    "        pattern = os.path.join(csv_folder, f'{prefix}*')\n",
    "        selected_files.extend(glob.glob(pattern))\n",
    "    index = 0\n",
    "\n",
    "    if not selected_files:\n",
    "        print(f'No files found for {region_name}')\n",
    "        return 0\n",
    "    else:\n",
    "        print(f'Found {len(selected_files)} IdVg files with {region_name}')\n",
    "\n",
    "    for file in selected_files:\n",
    "        print(f'Processing file: {file}')\n",
    "        data = pd.read_csv(file, header=None)\n",
    "        data[2][1:] = data[2][1:].apply(lambda x: re.sub(r'^-', '', x))\n",
    "\n",
    "        wks = op.new_sheet('w', f'{os.path.basename(file)}')\n",
    "        wks.from_list(0, data[1], lname='Vgs')\n",
    "        wks.from_list(1, data[2], lname='Ids')\n",
    "\n",
    "        if index == 0:\n",
    "            gr = op.new_graph(template=template_path)\n",
    "            gr.name = f'G_uniformity_{region_name}'\n",
    "            p1 = gr[0].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p1.color = color_index[index]\n",
    "            p2 = gr[1].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p2.color = color_index[index]\n",
    "        \n",
    "        if index == 1:\n",
    "            p3 = gr[0].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p3.color = color_index[index]\n",
    "            p4 = gr[1].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p4.color = color_index[index]\n",
    "\n",
    "        if index == 2:\n",
    "            p5 = gr[0].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p5.color = color_index[index]\n",
    "            p6 = gr[1].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p6.color = color_index[index]\n",
    "        \n",
    "        if index == 3:\n",
    "            p7 = gr[0].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p7.color = color_index[index]\n",
    "            p8 = gr[1].add_plot(wks, colx='Vgs', coly='Ids')\n",
    "            p8.color = color_index[index]\n",
    "        \n",
    "        index += 1\n",
    "    return 1\n",
    "\n",
    "for i, file_prefix_region in enumerate(file_prefix_regions):\n",
    "    if process_region(file_prefix_region, f'{(i + 2) * 100}L'):\n",
    "        print(f'Processing {(i + 2) * 100}L done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关闭origin界面\n",
    "if op.oext:\n",
    "    op.exit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
